<template>
  <div class="divBox relative">
    <el-card class="box-card" style="min-height: 80vh">
      <div slot="header" class="clearfix" style="margin-bottom: -20px ;">
        <div class="container">
          <el-form :inline="true" :model="queryForm" @submit.native.prevent>
            <!--            <el-form-item>-->
            <!--              <el-input v-model="queryForm.imgid" placeholder="请输入id" clearable @keyup.enter.native="search()"></el-input>-->
            <!--            </el-form-item>-->

            <el-form-item label="" >
              <el-select v-model="queryForm.imgblock" placeholder="请选择版块" class="width100"  filterable clearable>
                <el-option key="首页" label="首页" value="0"/>
                <el-option key="云玩" label="云玩" value="1"/>
                <el-option key="云玩" label="PC推荐" value="2"/>
                <el-option key="云玩" label="PC云玩" value="3"/>
                <el-option key="开屏页" label="开屏页" value="5"/>
              </el-select>
            </el-form-item>
            <el-form-item label="" >
              <el-select v-model="queryForm.imgtype" placeholder="请选择类型" class="width100"  filterable clearable>
                <el-option key="普通" label="无跳转" value="1"/>
                <el-option key="跳转H5" label="跳转H5" value="2"/>
                <el-option key="跳转App" label="跳转App" value="3"/>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="search()">查询</el-button>
              <el-button v-if="checkPermi(['imgentity:index:save'])" type="primary" @click="addOrUpdateHandle()">新增数据</el-button>
              <el-button v-if="checkPermi(['imgentity:index:delete'])"  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;" size="mini" :highlight-current-row="true" :header-cell-style=" {fontWeight:'bold'}">
        <el-table-column type="selection" header-align="center" align="center" width="50"/>
        <el-table-column prop="imgid" label="id" header-align="center" align="center" :show-overflow-tooltip="true">

        </el-table-column>
        <el-table-column prop="imgname" label="标题" header-align="center" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="content" label="描述" header-align="center" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="sort" label="排序" header-align="center" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="imgpath" label="图片" header-align="center" align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope" >
            <el-image style="width: 80px !important;height: 100% !important"  fit="scaleDown"
                      :src="scope.row.imgpath" :preview-src-list="[scope.row.imgpath]"/>
          </template>
        </el-table-column>
        <el-table-column prop="imgblock" label="版块" header-align="center" align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope" >
            <span v-if="scope.row.imgblock==0">首页</span>
            <span v-else-if="scope.row.imgblock==1">云玩</span>
            <span v-else-if="scope.row.imgblock==2">PC推荐</span>
            <span v-else-if="scope.row.imgblock==3">PC云玩</span>
            <span v-else-if="scope.row.imgblock==5">开屏页</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column prop="imgtype" label="类型" header-align="center" align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope" >
            <span v-if="scope.row.imgtype==1">无跳转</span>
            <span v-else-if="scope.row.imgtype==2">跳转H5</span>
            <span v-else-if="scope.row.imgtype==3">跳转App</span>
            <span v-else>-</span>
          </template>
        </el-table-column>

        <el-table-column prop="imgadderss" label="跳转" header-align="center" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="createtime" label="创建时间" header-align="center" align="center" width="150" :show-overflow-tooltip="true"/>
        <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="text"  v-if="checkPermi(['imgentity:index:update'])" size="small" @click="addOrUpdateHandle(scope.row.imgid)">修改</el-button>
            <el-button type="text"  v-if="checkPermi(['imgentity:index:delete'])" size="small" @click="deleteHandle(scope.row.imgid)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="tableForm.pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="tableForm.pageSize"
        :total="tableForm.total"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </el-card>
    <!-- 表单弹窗, 新增数据和修改数据 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './imgentity-add-and-update'
  import api from '@/api/api'
  import {checkPermi} from "@/utils/permission";

  export default {
    components: {
      AddOrUpdate
    },
    data () {
      return {
        dataList: [],
        queryForm: {
          imgid: ''
        },
        tableForm:{
          pageIndex: 1,
          pageSize: 10,
          total: 0,
        },
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    mounted () {
      this.getDataList()
    },
    methods: {
      checkPermi,
      search(){
        this.tableForm.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.dataListLoading = true
        let postData={
          'order':'desc',
          'column':'sort,imgid'
        }
        Object.assign(postData,this.queryForm, this.tableForm)
        //postData.key=postData.key.length==0?postData.key:'%'+postData.key+'%'
        api.post('rest/vabc/imgEntity/list',postData).then(res => {
          this.dataList=res.datalist
          this.tableForm.total=res.total
        }).finally(() => {
          this.dataListLoading = false
        });
      },
      // 每页数
      sizeChangeHandle (val) {
        this.tableForm.pageSize = val
        this.tableForm.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.tableForm.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.imgid
        })
        this.$confirm(`您确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let postData={ids:ids.join(',')};
          api.post('rest/vabc/imgEntity/delete',postData).then(res => {
            this.$message.success("删除成功")
            this.getDataList()
          })
        })
      }
    }
  }
</script>
